<?php
namespace app\mobile\controller;

use app\model\Util; 
use app\model\Theme; 
use app\model\Worker; 
use app\model\Relation; 

class Myordercheck1 extends Base
{
    public function Myordercheck1()
    {

global $_G, $_P;
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$system=$_G['system'];
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
$uid = intval($_P['uid']);
if($themeid < 0 || $uid < 0){
	return_msg('参数不正确');
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
    die(Util::showError("请确认活动地址是否正确"));
    return;
}
$url= $_G['siteroot'].$this->createMobileUrl('index',array("themeid"=>$themeid,"soft"=>$theme['softkey']));
$myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
if (empty($myworker)){
	if (empty($workerid)){
		if(!empty(Worker::getThemeWorkerByOpenidAlltheme($_G['openid'],1))){
			$theyworker = Worker::CopyThemeWorkerByUid($_G['member']['uid'],$themeid,1);//复制员工信息到新活动
			$workerid = $_G['member']['uid'];
		}
	}elseif (!empty($workerid)){
		$theyworker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
		if (empty($theyworker)){
			$theyworker = Worker::CopyThemeWorkerByUid($workerid,$themeid,1);//复制员工信息到新活动
		}
	}
}

$myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
if (empty($myworker)){
	if(!empty($themyrecord)){
		$workerid = $themyrecord['workerid'];
	}else{
		$workerid = intval($_P['workerid']);
	}
	if ($workerid==0){
		$relation = Relation::getByThemeId($themeid,$_G['openid']);
		$workerid = $relation['workerid'];
	}
	if (empty($workerid)){
		if(!empty(Worker::getThemeWorkerByOpenidAlltheme($_G['openid'],1))){
			$theyworker = Worker::CopyThemeWorkerByUid($_G['member']['uid'],$themeid,1);//复制员工信息到新活动
			$workerid = $_G['member']['uid'];
		}
	}elseif (!empty($workerid)){
		$theyworker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
		if (empty($theyworker)){
			$theyworker = Worker::CopyThemeWorkerByUid($workerid,$themeid,1);//复制员工信息到新活动
		}
	}
	$myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
}
if (empty($myworker)){
	return_msg('错误:您不是员工，没有权限') ;
	return;
}
$shopid = $myworker['company'];
if ($system['ruanjian']==3 && $shopid==0){
	return_msg('错误:您的员工状态不正确,请重新扫描【添加员工】的二维码') ;
	return;
}

$sql = "SELECT * FROM " . tablename (t_record) . "
									WHERE themeid =:themeid
											and uid = :uid and status > 0 order by type asc limit 1";
$order = db_find($sql, array(":themeid"=>$themeid,":uid"=>$uid));
if(empty($order) ){
	return_msg('错误:该客户还未付款') ;
}


$shop=db_find("SELECT * FROM ".tablename(t_hexiao)." WHERE uid = '{$uid}' and shopid = '{$shopid}' and themeid = '{$themeid}'");
if(!empty($shop) ){
	if ($shop['workerid']==-1){
		$hexiaoworker = array();
		$hexiaoworker['username']='后台';
	}else{
		$hexiaoworker=db_find("SELECT * FROM ".tablename(t_worker)." WHERE uid = '{$shop['workerid']}'");
	}
	return_msg('错误:已经于'.$shop['addtime'].'由本公司'.$hexiaoworker['username'].'核销过',$url, 'error') ;
}else{
	$data ['uniacid'] = $_G['mpid'];
	$data ['uid'] = $uid;
	$data ['shopid'] = $shopid;
	$data ['themeid'] = $themeid;
	$data ['workerid'] = $_G['member']['uid'];
	$data ['addtime'] = date('Y-m-d H:i:s');
	db_insert ( t_hexiao, $data );
	return_msg('成功核销',$url, 'success') ;
}
}
}